package com.gragra.outHelpers;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
/**
 * Helper Class that writes Strings to a BufferedWriter and ensures that every time
 * write is called the output will be on its own line.
 * @author Christoph Teichmann
 * @version 0.1
 */
public class OutHelper
{
	/**
	 * indicates whether the first output line is yet to be written
	 */
	private boolean first = true;
	/**
	 * the output to which strings are written
	 */
	private final BufferedWriter out;
	/**
	 * creates a new Outputhelper in which the String is used as the location of 
	 * a file to which a BufferedWriter is created.
	 * @param first
	 * @param out
	 * @throws IOException 
	 */
	public OutHelper(final String o) throws IOException
	{
		super();
		this.out = new BufferedWriter(new FileWriter(o));
	}
	/**
	 * writes the given string to the output file on a new line
	 * @param line
	 * @throws IOException
	 */
	public void write(final String line) throws IOException
	{
		if(first)
		{first = false;}
		else
		{out.newLine();}
		out.write(line);
	}
	/**
	 * closes the connection to the underlying file.
	 *
	 * @throws IOException
	 */
	public void close() throws IOException
	{
		out.close();
	}
}